/*
 * author: coke
 * email:  black.angel.liu@gmail.com
 * des:    跨浏览器通用事件对象
 */

var EventUtil = {
	addHandler:function(element,type,handler){
		//DOM2 EVENT
		if(element.addEventListener){
			element.addEventListener(type,handler,false);
		}
		//IE EVENT
		else if(element.attachEvent){
			element.attachEvent("on" + type,handler);
		}
		//DOM0 EVENT
		else{
			element["on"+type] = handler;
		}
	},
	getEvent:function(event){
		return event ? event : window.event;
	},
	getTarget:function(event){
		return event.target || event.srcElement;
	},
	//阻止事件的默认行为
	preventDefault:function(event){
		if(event.preventDefault){
			event.preventDefault();
		}else{
			event.returnValue = false;
		}
	},
	//删除事件
	removeHandler:function(element,type,handler){
		if(element.removeEventListener){
			element.removeEventListener(type,handler,false);
		}
		//IE EVENT
		else if(element.detachEvent){
			element.detachEvent("on" + type,handler);
		}
		//DOM0 EVENT
		else{
			element["on"+type] = null;
		}
	},
	//获得事件目标，用于 mouseover 事件等.
	getRelatedTarget:function(event){
		if(event.relatedTarget){
			return event.relatedTarget;
		}else if(event.toElement){
			return event.toElement;
		}else if(event.fromElement){
			return event.fromElement;
		}else{
			return null;
		}
	},
	//获得事件源对相
	getCharCode:function(event){
		if(typeof event.charCode == "number"){
			return event.charCode;
		}else{
			return event.keyCode
		}	
	},
	//阻止事件冒泡
	stopPropagation:function(event){
		if(event.stopPropagation){
			event.stopPropagation();
		}else{
			event.cancelBubble = true;
		}
	}
};